from flask import Flask, render_template
import random
from datetime import datetime, timedelta

app = Flask(__name__)

# 生成模拟数据（最近12小时的时间、温度、湿度）
def generate_simulation_data():
    times = [(datetime.now() - timedelta(hours=i)).strftime("%H:%M") for i in range(12)][::-1]
    temps = [round(random.uniform(22.5, 28.3), 1) for _ in range(12)]  # 温度：22.5-28.3℃
    humids = [round(random.uniform(45.2, 62.8), 1) for _ in range(12)]  # 湿度：45.2-62.8%
    return times, temps, humids

# 设备状态（模拟实时状态）
device_states = {
    "run_status": "正常运行",
    "run_mode": "自动模式",
    "power_status": "电源接通"
}

# 4个设备卡片数据
device_cards = [
    {"name": "设备A", "id": "DEV-001", "status": "在线", "color": "#4CAF50"},
    {"name": "设备B", "id": "DEV-002", "status": "在线", "color": "#4CAF50"},
    {"name": "设备C", "id": "DEV-003", "status": "离线", "color": "#F44336"},
    {"name": "设备D", "id": "DEV-004", "status": "在线", "color": "#4CAF50"}
]

@app.route('/')
def index():
    times, temps, humids = generate_simulation_data()
    return render_template('dashboard.html',
                           times=times, temps=temps, humids=humids,
                           states=device_states, cards=device_cards)

if __name__ == '__main__':
    app.run(debug=True)